package stalker;

import java.io.*;
import java.net.*;
import com.sun.org.apache.xerces.internal.dom.AttributeMap;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.Properties;
import javax.swing.SwingWorker;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.jdesktop.application.FrameView;
//import org.jdesktop.swingworker.SwingWorker;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ClientMessageRcv extends SwingWorker<Void, ClientMessageRcv> {

    private ServerSocket ss;
    private Socket sock;
    StalkerView Clientfrm;
    private Mensagem MsgfromClient;
    private byte[] ByteBuffer;
    private final static String newline = "\n";

    public ClientMessageRcv(StalkerView frm, int cliRcvPort) {
        try {
            ss = new ServerSocket(cliRcvPort);
            Clientfrm = frm;

        } catch (Exception e) {
        }

    }

    public Void doInBackground() {

        //System.out.println("Nova conversa iniciando ...");

        try {
            ObjectInputStream in;
            //ObjectOutputStream out;

            while (true) {
                sock = ss.accept(); // socket ativo criado

                //System.out.println(" diz:");

                //out = new ObjectOutputStream(sock.getOutputStream());
                in = new ObjectInputStream(sock.getInputStream());

                ByteBuffer = new byte[1024];
                in.read(ByteBuffer);

                MsgfromClient = new Mensagem();
                MsgfromClient.convertToClass(ByteBuffer);

                switch (MsgfromClient.getCdTransacao()) {
                    case 6:
                        BeginChat();
                        break;
                    case 7:
                        PrintMessage();
                        break;
                    case 8:
                        FinishChat();
                        break;
                }
                Clientfrm.txtChatArea.setCaretPosition(Clientfrm.txtChatArea.getText().length());
                        
                        in.close();
                    //out.close();
                        sock.close();
                }
            }  catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    private void BeginChat() {
        Clientfrm.ClientIp = MsgfromClient.intToIp(MsgfromClient.getIp());
        Clientfrm.ClientPort = MsgfromClient.getPorta();
        
        Clientfrm.NickClient = MsgfromClient.getTexto();
        Clientfrm.txtNome.setText(Clientfrm.NickClient);
        Clientfrm.txtChatArea.append(Clientfrm.NickClient + " iniciou uma conversa com você..." + newline );
        
        Clientfrm.txtNome.setEnabled(false);
        Clientfrm.btnConectar.setEnabled(false);
        Clientfrm.txtMessage.setEnabled(true);
        Clientfrm.btnEnviar.setEnabled(true);
        Clientfrm.btnFim.setEnabled(true);
     }

    private void PrintMessage() {
        Clientfrm.txtChatArea.append(Clientfrm.NickClient + " diz: " + newline + MsgfromClient.getTexto() + newline);
    }

    private void FinishChat() {
        Clientfrm.ClientIp = "";
        Clientfrm.ClientPort = 0;
        
        Clientfrm.txtChatArea.append(Clientfrm.NickClient + " saiu da conversa." + newline + newline);
        Clientfrm.NickClient = "";
        
        Clientfrm.txtNome.setEnabled(true);
        Clientfrm.btnConectar.setEnabled(true);
        Clientfrm.txtNome.setText("");
        Clientfrm.txtMessage.setEnabled(false);
        Clientfrm.btnEnviar.setEnabled(false);                
        Clientfrm.btnFim.setEnabled(false);        
    }
}
